﻿"use strict";
// 相同端口
var connection = new signalR.HubConnectionBuilder().withUrl("/chatHub").build();
// 不同端口
const verificationConnection = new signalR.HubConnectionBuilder()
    .withUrl("http://localhost:5165/Hubs/PerformanceVerificationHub") //指定信号服务器的URL
    .configureLogging(signalR.LogLevel.Information)
    .build();

verificationConnection.start()
    .then(function () {
        console.log('SignalR连接成功');
    })
    .catch(function (error) {
        console.log('SignalR连接失败：' + error);
    });

//Disable the send button until connection is established.
document.getElementById("sendButton").disabled = true;
//document.getElementById("sendAutoButton").disabled = true;

connection.on("ReceiveMessage", function (user, message) {
    var li = document.createElement("li");
    document.getElementById("messagesList").appendChild(li);
    // We can assign user-supplied strings to an element's textContent because it
    // is not interpreted as markup. If you're assigning in any other way, you 
    // should be aware of possible script injection concerns.
    li.textContent = `${user} says ${message}`;
});

connection.start().then(function () {
    document.getElementById("sendButton").disabled = false;
}).catch(function (err) {
    return console.error(err.toString());
});


document.getElementById("sendButton").addEventListener("click", function (event) {
    var user = document.getElementById("userInput").value;
    var message = document.getElementById("messageInput").value;
    connection.invoke("SendMessage", user, message).catch(function (err) {
        return console.error(err.toString());
    });
    event.preventDefault();
});




verificationConnection.on("ReceiveAutoStatResult", function (notifyVerif) {
    var li = document.createElement("li");
    document.getElementById("messagesList").appendChild(li);
    // We can assign user-supplied strings to an element's textContent because it
    // is not interpreted as markup. If you're assigning in any other way, you 
    // should be aware of possible script injection concerns.
    li.textContent = `verificationConnection says ${notifyVerif}`;
});

//verificationConnection.start().then(function () {
//    document.getElementById("sendAutoButton").disabled = false;
//}).catch(function (err) {
//    return console.error(err.toString());
//});

document.getElementById("sendAutoButton").addEventListener("click", function (event) {
    var notifyVerif = document.getElementById("notifyVerifMessageInput").value;
    console.log("SendAutoStatResult");
    verificationConnection.invoke("SendAutoStatResult", notifyVerif).catch(function (err) {
        return console.error(err.toString());
    });
    event.preventDefault();
});